import 'package:flutter/material.dart';
import 'package:osc/data/repository/repository.dart';
import 'package:osc/models/entity/osc_soft_banner_entity.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/widgets/small_widgets.dart';
import 'package:osc/utils/navigator_util.dart';
import 'package:osc/utils/pd_util.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

import '../image_loader.dart';


class OscProjectBannerWidget extends StatefulWidget{
  final double height;
  final int delay;
  OscProjectBannerWidget({this.height = Dimens.view_blog_banner_height, this.delay = 5000});

  @override
  State<StatefulWidget> createState() {
    return new OscProjectBannerWidgetState(height, delay);
  }
}

class OscProjectBannerWidgetState extends State<OscProjectBannerWidget>{
  final double height;
  final int delay;
  OscProjectBannerWidgetState(this.height, this.delay);

  int itemCount = 0;
  List<OscSoftBanner> bannerList;


  @override
  void initState() {
    super.initState();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: height,
      child: Swiper(
        onTap: (int index){
          pushWeb(context, index);
        },
        itemBuilder: (BuildContext context, int index){
          return getItem(index);
        },
        itemCount: itemCount,
        itemHeight: height,
        autoplay: itemCount>1?true:false,
        autoplayDelay: delay,
        pagination: SwiperPagination(
          alignment: Alignment.bottomRight,
          builder: DotSwiperPaginationBuilder(
            color: Colors.white,
            activeColor: Colors.green,
            size: Dimens.view_radius_size_normal,
            activeSize: Dimens.view_radius_size_normal,
          )
        ),
      ),
    );
  }

  Widget getItem(int index){
    OscSoftBanner banner;
    String url = "";
    if(bannerList != null){
      banner = bannerList.elementAt(index);
      if(banner != null){
        url = PdUtil.getFullUrl(banner.img);
      }
    }
    return Container(
      child: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          ImageLoader(url: url, fit: BoxFit.cover,),
          Positioned(
            bottom: 0,
              child: Padding(
                  padding: EdgeInsets.all(Dimens.gap_view_bigger),
                child: Column(
                  children: <Widget>[
                    SmallWidgets.buildText(banner.name, Colors.white, Dimens.font_sp14, fontWeight: FontWeight.bold),
                    SmallWidgets.buildText(banner.detail, Colors.white, Dimens.font_sp12),
                  ],
                ),
              ),
          ),
        ],
      ),
    );
  }

  pushWeb(BuildContext context, int index){
    String url = "";
    String title;
    if(bannerList != null){
      OscSoftBanner banner = bannerList.elementAt(index);
      if(banner != null){
        url = banner.href;
        title = banner.name;
        NavigatorUtil.pushUrl(context, url: url, title: title);
      }
    }
  }

  loadData() async {
    await new HttpRepository().getProjectBanner().then((resp) {
      OscSoftBannerEntity entity = resp.result;
      bannerList = entity?.data;
      if(bannerList != null){
        itemCount = bannerList.length;
      }
    });

    if(mounted){
      setState(() {});
    }
  }

}